package com.dxg.msns.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dxg.msns.user.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
        @Select("select uid,uname,avatar_url from user where uid = #{uid}")
        @Results(id = "userMap", value = {
                        @Result(id = true, column = "uid", property = "uid"),
                        @Result(column = "uname", property = "uname"),
                        @Result(column = "avatar_url", property = "avatarUrl"),
        })
        User queryNameAvatarById(@Param("uid") String uid);


        /**
         * 根据 uid 查询歌手（is_singer = 1）
         *
         * @param uid 用户 id
         * @return 歌手用户信息，若不是歌手返回 null
         */
        @Select("SELECT * FROM user WHERE uid = #{uid} AND is_singer = 1")
        User selectSingerByUid(@Param("uid") String uid);




        /**
         * 修改密码
         * 
         * @param id
         * @param newPwd
         * @param salt
         */
        @Update("update user set upassword = #{newPwd} , salt = #{salt} where id=#{id}")
        void changePwdById(@Param("id") Integer id, @Param("newPwd") String newPwd, @Param("salt") String salt);

        /**
         * 修改头像
         * 
         * @param uid
         * @param avatarUrl
         */
        @Update("update user set avatar_url = #{avatarUrl} where uid = #{uid}")
        @ResultMap("userMap")
        void changeAvatar(@Param("uid") String uid, @Param("avatarUrl") String avatarUrl);

        /**
         * 根据用户名查询用户
         * 
         * @param uname
         * @return
         */
        @Select("select * from user where uname = #{uname}")
        List<User> selectByUserName(String uname);

        /**
         * 根据用户名查询用户
         * 
         * @param uname
         * @return
         */
        @Select("select * from user where uname = #{uname}")
        User selectByUName(String uname);

        /**
         * 根据id变更用户头像
         *
         * @param uid
         * @return
         */
        @Update("update user set avatar_url = #{avatarUrl} where uid = #{uid}")
        void changeAvatarByUid(@Param("uid") String uid, @Param("avatarUrl") String avatarUrl);


        /**
         * 模糊查询用户（按用户名查询）
         *
         * @param uname 用户名关键字
         * @return 用户列表
         */
        @Select("SELECT * FROM user WHERE uname LIKE CONCAT('%', #{uname}, '%')")
        List<User> queryUsersByName(@Param("uname") String uname);


        @Select("SELECT * FROM user WHERE uid = #{uid}")
        User selectByUid(@Param("uid") String uid);


        @UpdateProvider(type = UserProvider.class, method = "updateUserByUid")
        void updateByUidSelective(User user);

        class UserProvider {
                public String updateUserByUid(User user) {
                        StringBuilder sql = new StringBuilder("UPDATE user SET ");

                        if (user.getUname() != null) {
                                sql.append("uname = #{uname}, ");
                        }
                        if (user.getEmail() != null) {
                                sql.append("email = #{email}, ");
                        }
                        if (user.getPhone() != null) {
                                sql.append("phone = #{phone}, ");
                        }
                        if (user.getSex() != null) {
                                sql.append("sex = #{sex}, ");
                        }
                        if (user.getBirthday() != null) {
                                sql.append("birthday = #{birthday}, ");
                        }
                        if (user.getAvatarUrl() != null) {
                                sql.append("avatar_url = #{avatarUrl}, ");
                        }
                        if (user.getDescription() != null) {
                                sql.append("description = #{description}, ");
                        }
                        if (user.getIsSinger() != null) {
                                sql.append("is_singer = #{isSinger}, ");
                        }
                        // 去掉末尾多余的逗号
                        sql.setLength(sql.length() - 2);

                        sql.append(" WHERE uid = #{uid}");

                        return sql.toString();
                }
        }


}
